#!/bin/bash
# This file is part of the jetson_stats package (https://github.com/rbonghi/jetson_stats or http://rnext.it).
# Copyright (c) 2019-2023 Raffaello Bonghi.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# Emulator for NVIDIA Jetson tegrastats

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`

usage()
{
    if [ "$1" != "" ]; then
        echo "${red}$1${reset}"
    fi

    echo "tegrastats, Emulator of NVIDIA Jetsons"
    echo "Usage:"
    echo "$0 [options]"
    echo "options,"
    echo "   -h|--help    | This help"
    echo "   --interval   | Frequency interval"
    echo "   --test-model | Test the jetson version (Does not exist in real!!)"
}

tegrastats_out()
{
    local JETSON_MODEL=$1
    # Generate random value for GPU
    local RAND_GPU=$(( $RANDOM % 100 + 0 ))
    local RAND_GPU2=$(( $RANDOM % 100 + 0 ))

    if [ "$JETSON_MODEL" = "TX1" ] ; then
        # Jetson TX1
        echo "RAM 385/3984MB (lfb 779x4MB) CPU [$(($RANDOM%100+0))%@204,$(($RANDOM%100+0))%@204,$(($RANDOM%100+0))%@204,$(($RANDOM%100+0))%@204] AO@31C CPU@25C GPU@22C PLL@21.5C Tdiode@28.75C PMIC@100C Tboard@29C thermal@23.5C"
    elif [ "$JETSON_MODEL" = "TX2" ] ; then
        # Jetson TX2
        echo "RAM 707/7854MB (lfb 1659x4MB) CPU [$(($RANDOM%100+0))%@345,$(($RANDOM%100+0))%@345,$(($RANDOM%100+0))%@345,$(($RANDOM%100+0))%@345,$(($RANDOM%100+0))%@345,$(($RANDOM%100+0))%@345] BCPU@30C MCPU@30C GPU@28.5C PLL@30C Tboard@26C Tdiode@27.5C PMIC@100C thermal@29.4C VDD_IN 1038/1134 VDD_CPU 153/153 VDD_GPU 153/153 VDD_SOC 384/384 VDD_WIFI 38/124 VDD_DDR 192/192"
    elif [ "$JETSON_MODEL" = "Xavier" ] ; then
        # Jetson Xavier
        echo "RAM 407/15823MB (lfb 3702x4MB) SWAP 0/7911MB (cached 0MB) CPU [$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265] EMC_FREQ 0% GR3D_FREQ $RAND_GPU% AO@26.5C GPU@28C iwlwifi@30C Tdiode@31.25C PMIC@100C AUX@26.5C CPU@27.5C thermal@27.45C Tboard@27C GPU 1155/1155 CPU 385/385 SOC 2504/2503 CV 0/0 VDDRQ 192/192 SYS5V 2692/2692"
    elif [ "$JETSON_MODEL" = "Orin" ] ; then
        # Jetson Orin
        echo "01-03-2023 16:10:22 RAM 2257/30536MB (lfb 5392x4MB) SWAP 0/15268MB (cached 0MB) CPU [$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,0%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729] EMC_FREQ 0% GR3D_FREQ $RAND_GPU% CV0@-256C CPU@41.375C Tboard@29C SOC2@39C Tdiode@30.75C SOC0@38.906C CV1@-256C GPU@-256C tj@41.468C SOC1@38.843C CV2@-256C"
    elif [ "$JETSON_MODEL" = "Nano" ] ; then
        # Jetson Nano
        echo "RAM 330/3964MB (lfb 758x4MB) SWAP 0/1982MB (cached 0MB) CPU [$(($RANDOM%100+0))%@102,$(($RANDOM%100+0))%@102,$(($RANDOM%100+0))%@102,$(($RANDOM%100+0))%@102] EMC_FREQ 0% GR3D_FREQ $RAND_GPU% PLL@20.5C CPU@23C PMIC@100C GPU@22.5C AO@27C thermal@22.5C POM_5V_IN 933/933 POM_5V_GPU 0/0 POM_5V_CPU 84/84"
    else
        # Extra
        echo "RAM 4722/7844MB (lfb 1x512kB) CPU [$(($RANDOM%100+0))%@2035,$(($RANDOM%100+0))%@2034,$(($RANDOM%100+0))%@2034,$(($RANDOM%100+0))%@2035,$(($RANDOM%100+0))%@2035,$(($RANDOM%100+0))%@2035] SWAP 149/1024MB (cached 7MB) EMC_FREQ 2%@1866 GR3D_FREQ $RAND_GPU%@1300 APE 150 MTS fg 0% bg 0% BCPU@-45C MCPU@-45C GPU@-51C PLL@45C AO@47.5C Tboard@37C Tdiode@46.75C PMIC@100C thermal@46.4C VDD_IN 14025/14416 VDD_CPU 2209/2538 VDD_GPU 6854/6903 VDD_SOC 1371/1370 VDD_WIFI 19/19 NVENC 716 NVDEC 716 VDD_DDR 2702/2702"
    fi
}

tegrastats_out_sudo()
{
    local JETSON_MODEL=$1
    # Generate random value for GPU
    local RAND_GPU=$(( $RANDOM % 100 + 0 ))
    local RAND_GPU2=$(( $RANDOM % 100 + 0 ))

    if [ "$JETSON_MODEL" = "TX1" ] ; then
        # Jetson TX1 (SUDO)
        echo "RAM 388/3984MB (lfb 779x4MB) IRAM 0/252kB(lfb 252kB) CPU [$(($RANDOM%100+0))%@1734,$(($RANDOM%100+0))%@1734,$(($RANDOM%100+0))%@1734,$(($RANDOM%100+0))%@1734] EMC_FREQ 0%@1600 GR3D_FREQ $RAND_GPU%@998 APE 25 AO@30.5C CPU@25.5C GPU@23C PLL@21.5C Tdiode@28.5C PMIC@100C Tboard@28C thermal@24C"
    elif [ "$JETSON_MODEL" = "TX2" ] ; then
        # Jetson TX2 (SUDO)
        echo "RAM 696/7854MB (lfb 1675x4MB) CPU [$(($RANDOM%100+0))%@345,$(($RANDOM%100+0))%@345,$(($RANDOM%100+0))%@345,$(($RANDOM%100+0))%@345,$(($RANDOM%100+0))%@345,$(($RANDOM%100+0))%@345] EMC_FREQ 1%@665 GR3D_FREQ $RAND_GPU%@140 APE 150 MTS fg 0% bg 0% BCPU@30C MCPU@30C GPU@28C PLL@30C Tboard@26C Tdiode@27C PMIC@100C thermal@29.2C VDD_IN 1383/1383 VDD_CPU 153/191 VDD_GPU 76/76 VDD_SOC 384/384 VDD_WIFI 364/335 VDD_DDR 192/201"
    elif [ "$JETSON_MODEL" = "Xavier" ] ; then
        # Jetson Xavier (SUDO)
        echo "RAM 408/15823MB (lfb 3702x4MB) SWAP 0/7911MB (cached 0MB) CPU [$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265,$(($RANDOM%100+0))%@2265] EMC_FREQ 0%@2133 GR3D_FREQ $RAND_GPU%@1377 APE 150 MTS fg 0% bg 0% AO@26.5C GPU@28C iwlwifi@30C Tdiode@31.25C PMIC@100C AUX@27C CPU@28C thermal@27.45C Tboard@27C GPU 1155/1155 CPU 385/385 SOC 2502/2502 CV 0/0 VDDRQ 192/192 SYS5V 2692/2692"
    elif [ "$JETSON_MODEL" = "Orin" ] ; then
        # Jetson Orin (SUDO)
        echo "01-03-2023 16:07:54 RAM 2257/30536MB (lfb 5392x4MB) SWAP 0/15268MB (cached 0MB) CPU [$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729,$(($RANDOM%100+0))%@729] EMC_FREQ 0%@204 GR3D_FREQ $RAND_GPU%@0 GR3D2_FREQ $RAND_GPU2%@0 NVJPG1 729 VIC_FREQ 115 APE 233 CV0@-256C CPU@41.437C Tboard@29C SOC2@39.062C Tdiode@30.75C SOC0@39.031C CV1@-256C GPU@-256C tj@41.656C SOC1@38.937C CV2@-256C VDD_GPU_SOC 2390mW/2390mW VDD_CPU_CV 796mW/796mW VIN_SYS_5V0 3522mW/3522mW NC 0mW/0mW VDDQ_VDD2_1V8AO 301mW/301mW NC 0mW/0mW"
    elif [ "$JETSON_MODEL" = "Nano" ] ; then
        # Jetson Nano (SUDO)
        echo "RAM 331/3964MB (lfb 758x4MB) SWAP 0/1982MB (cached 0MB) IRAM 0/252kB(lfb 252kB) CPU [$(($RANDOM%100+0))%@102,$(($RANDOM%100+0))%@102,$(($RANDOM%100+0))%@102,$(($RANDOM%100+0))%@102] EMC_FREQ 3%@204 GR3D_FREQ $RAND_GPU%@76 APE 25 PLL@20.5C CPU@23.5C PMIC@100C GPU@22.5C AO@27.5C thermal@23C POM_5V_IN 933/933 POM_5V_GPU 0/0 POM_5V_CPU 84/105"
    else
        # Extra
        echo "RAM 4722/7844MB (lfb 1x512kB) CPU [$(($RANDOM%100+0))%@2035,$(($RANDOM%100+0))%@2034,$(($RANDOM%100+0))%@2034,$(($RANDOM%100+0))%@2035,$(($RANDOM%100+0))%@2035,$(($RANDOM%100+0))%@2035] SWAP 149/1024MB (cached 7MB) EMC_FREQ 2%@1866 GR3D_FREQ $RAND_GPU%@1300 APE 150 MTS fg 0% bg 0% BCPU@-45C MCPU@-45C GPU@-51C PLL@45C AO@47.5C Tboard@37C Tdiode@46.75C PMIC@100C thermal@46.4C VDD_IN 14025/14416 VDD_CPU 2209/2538 VDD_GPU 6854/6903 VDD_SOC 1371/1370 VDD_WIFI 19/19 NVENC 716 NVDEC 716 VDD_DDR 2702/2702"
    fi
}

main()
{
    local TEST_MODEL_FOLDER="/tmp/jetson_model"
    local JETSON_MODEL=""
    local INTERVAL=500
    
    # Decode all information from startup
    while [ -n "$1" ]; do
        case "$1" in
            --interval)
                INTERVAL=$2
                shift 1
                ;;
            -h|--help)
                usage
                exit 0
                ;;
            --test-model)
                echo $2 > $TEST_MODEL_FOLDER
                echo "${green}Write $2 in $TEST_MODEL_FOLDER${reset}"
                exit 0
                ;;
            *)
                usage "[ERROR] Unknown option: $1"
                exit 1
            ;;
        esac
            shift 1
    done
    
    # Evaluate time
    INTERVAL=$(echo "scale=2; $INTERVAL/1000" | bc)
    # Read Jetson Model emulator
    if [ -f $TEST_MODEL_FOLDER ] ; then
        JETSON_MODEL=$(sed '1q;d' $TEST_MODEL_FOLDER)
    fi
    # Check if running with sudo or not
    IS_SUDO=false
    if [ $(id -u) -eq 0 ] ; then
        IS_SUDO=true
    fi
    
    while true; do 
        # Print output tegrastats
        if $IS_SUDO ; then
            tegrastats_out_sudo $JETSON_MODEL
        else
            tegrastats_out $JETSON_MODEL
        fi
        # Go to sleep for an interval
        sleep $INTERVAL
    done
}

main $@
exit 0

# EOF
